{extend name="public:base" title="$list_base.title" /}
{block name="cont"}
{// 检测是否存在顶部导航}
{notempty name="list_nav"}
<div class="layout cx-fex-c cx-borbottom cx-mag-b10">
    {volist name="list_nav.list" id="cx" empty=""}
    <a href="{$cx.uri}" class="cx-button {eq name='cx.id|default=0' value='$list_nav.default'}cx-bg-blue{else /}cx-bor-blue{/eq}">{$cx.title}</a>
    {/volist}
</div>
{/notempty}
<div class="layout cx-fex cx-fex-itemsc cx-mag-b10">
    <div class="cx-fex-l cx-fex-itemsc">
        {eq name="list_base.add|default='0'" value="1"}
        <a href="{:url('create')}" class="cx-button cx-bg-blue">添加{$list_base.title|default=""}</a>
        {else /}
        {notempty name="list_base.add"}
        <a href="{$list_base.add}" class="cx-button cx-bg-blue">{$list_base.addtitle|default="添加"}{$list_base.title|default=""}</a>
        {/notempty}
        {/eq}
        {notempty name="list_top"}
        {volist name="list_top" id="cx"}
        <a {eq name="cx.ids|default='0'" value="1"}data-uri="{$cx.uri}" data-ids="ids"{else /}href="{$cx.uri}"{/eq} class="{$cx.class|default='cx-button cx-bg-blue cx-mag-l5'}">{$cx.title}</a>
        {/volist}
        {/notempty}
    </div>
    {notempty name="list_search"}
    <form class="layui-form cx-fex-r cx-form-group cx-xs10" action="{$list_search.uri}" method="post">
        {notempty name="list_search.filed"}
        <select name="{$list_search.filedname|default='filed'}">
            {volist name="list_search.filed" id="cx"}
            <option value="{$cx.value}">{$cx.title}</option>
            {/volist}
        </select>
        {/notempty}
        <div class="cx-form-itemnowarp cx-xs12">
            <input type="text" name="keyword" placeholder="请输入搜索词..." value="" class="cx-ipt">
            <div class="cx-ipt-btn">
                <button class="cx-button cx-bg-red">确定</button>
            </div>
        </div>
    </form>
    <script>
        layui.use('form', function(){
            let layform = layui.form;
            layform.render('select');
            layform.on('submit(formDemo)', function(data){
                layer.msg(JSON.stringify(data.field));
                return false;
            });
        });
    </script>
    {/notempty}
</div>
<div class="layout map-table-box">
<table class="cx-table map-table">
    <thead>
        <tr>
            {neq name="list_base.idsee|default='1'" value="0"}
            <th {empty name='list_top'}class="cx-text-center"{/empty} style="width:80px;">
                {notempty name="list_top"}<input type="checkbox" class="_checkbox">{/notempty}
                {:strtoupper($list_base.id)}
            </th>
            {/neq}
            {// 开始循环显示字段名}
            {volist name=":WormCms('filed@FiledsToTable',$list_file)" id="cx" empty=""}
            <th class="{$cx.class|default=''}" {notempty name="cx.width"}style="width:{$cx.width}px;"{else /}style="min-width:150px;"{/notempty}>{$cx.title}</th>
            {/volist}
            {// 判断是否存在自订义右侧按钮}
            {present name="list_rightbtn"}
            {volist name=":WormCms('filed@BtnToTable',$list_rightbtn)" id="cx" empty=""}
            <th class="cx-text-center" {notempty name="cx.width"}style="width:{$cx.width}px;"{/notempty}>
                {$cx.title}
            </th>
            {/volist}
            {else /}
            {eq name="list_base.edit|default='0'" value="1"}<th class="cx-text-center" width="80">编辑</th>{/eq}
            {eq name="list_base.del|default='0'" value="1"}<th class="cx-text-center" width="80">删除</th>{/eq}
            {/present}
        </tr>
    </thead>
    <tbody>
        {volist name="listdb" id="cx" empty=""}
        <tr onmouseover="this.classList.add('open')" onmouseout="this.classList.remove('open')" {notempty name="list_base.list_fold"}{eq name="list_base.list_fold|default='0'" value="1"} data-id="{$cx[$list_base.id]}" data-pid="{$cx.pid}"{/eq}{/notempty}>
            {neq name="list_base.idsee|default='1'" value="0"}
            <td {empty name='list_top'}class="cx-text-center"{/empty}>
                {// 判断是否存在批量处理 包括编辑与删除}
                {notempty name="list_top"}<input type="checkbox" name="_ids[]" value="{$cx[$list_base.id]}">{/notempty}
                {$cx[$list_base.id]}
            </td>
            {/neq}
            {volist name=":WormCms('filed@FiledsToTable',$list_file,$cx)" id="c" empty=""}
            <td class="{$c.class|default=''}">
                {// 判断是否为折叠表格}
                {eq name="c.type|default=''" value="fold"}
                <i class="cx-icon _fold cx-iconjinzhi cx-mag-r5 cx-text-red-6 cx-opacity"></i>
                {/eq}
                {$c.value|raw}
            </td>
            {/volist}
        {// 判断是否存在自订义右侧按钮}
            {present name="list_rightbtn"}
            {volist name=":WormCms('filed@BtnToTable',$list_rightbtn,$cx)" id="c" empty=""}
            <td class="cx-text-center">
                {$c.value|raw}
            </td>
            {/volist}
            {else /}
            {eq name="list_base.edit|default='0'" value="1"}<td class="cx-text-center"><a href="{:url('edit',['id' => $cx[$list_base.id]])}"><i class="cx-icon cx-iconbianji cx-text-f16"></i></a></td>{/eq}
            {eq name="list_base.del|default='0'" value="1"}<td class="cx-text-center"><a class="cx-text-red _delete" data-id="{$cx[$list_base.id]}"><i class="cx-icon cx-icondelete cx-text-f16"></i></a></td>{/eq}
            {/present}
        </tr>
        {/volist}
    </tbody>
</table>
</div>
<div id="pages" class="layout cx-fex-c cx-pad-a20 cx-page"></div>
{/block}
{block name="foot_js"}
<script>
    let thlist = $(`.map-table th`),
        bw = $(`.map-table-box`).width() - 5,
        w = 0;
    $.each(thlist,function (index,item) {
        let _w = parseInt(item.style.width);
        _w = isNaN(_w) ? 120 : _w;
        w = w * 1 + _w * 1;
    });
    $(`.map-table`).width(w > bw ? w : bw);
</script>
{// 检测是否需要分页 }
{notempty name="jspage.total"}
<script>
    layui.use('laypage', function(){
        var laypage = layui.laypage,
            getUrlPapram = function (name) {
                let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"),
                    r = window.location.search.substr(1).match(reg); //匹配目标参数
                if (r != null) return unescape(r[2]);
                return null;
            };
        //执行一个laypage实例
        laypage.render({
            elem: 'pages',
            count: "{$jspage.total}",
            limit: "{$jspage.per_page}",
            limits:['20','50','100'],
            curr:  "{$jspage.current_page}",
            jump: function(obj, first){
                let url = window.location.href;
                if(url.indexOf('page=') > 0){
                    let v = getUrlPapram('page');
                    url = v != null ? url.replace(`page=${v}`,`page=${obj.curr}`) : url.replace(`page=`,`page=${obj.curr}`);
                }else{
                    url = url.indexOf("?") > 0 ? url + `&page=${obj.curr}` : url + `?page=${obj.curr}`;
                }
                if(!first){
                    window.location.href = url;
                }
            }
        });
    });
</script>
{/notempty}
{// 检测是否需要折叠表格}
{eq name="list_base.list_fold|default='0'" value="1"}
<script>
    let tr_list = $(`.cx-table`).find(`tr`);
    function table_fold() {
        $.each(tr_list,function (index,item) {
            let d = $(item).data();
            if(d.pid != '0'){
                table_addicon(d.pid);
            }
        });
    }
    table_fold();
    function table_addicon(pid,leve = 'a') {
        let add_name = leve === 'a' || leve === 's' ? 'cx-icondel' : 'cx-iconjia',
            del_name = leve === 'a' ? 'cx-iconjinzhi cx-opacity' : leve === 's'  ? 'cx-iconjia' : 'cx-icondel';
        $.each(tr_list,function (index,item) {
            let d = $(item).data();
            if(pid === d.id){
                $(item).find(`i._fold`).hasClass(add_name) ? '' : $(item).find(`i._fold`).addClass(add_name).removeClass(del_name);
            }
            if(pid === d.pid && leve != 'a') {
                leve === 'd' ? $(item).hide() : $(item).show();
                leve === 'd' ? table_addicon(d.id,leve) : '';
            }
        });
    }
    $(`i._fold`).click(function () {
        let tr = $(this).closest(`tr`),
            d = $(tr).data();
        if(!$(this).hasClass(`cx-iconjinzhi`)){
            table_addicon(d.id,$(this).hasClass(`cx-icondel`) ? 'd' : 's');
        }
    });
</script>
{/eq}
{// 检测快速编辑}
{eq name="list_base.list_edit|default='0'" value="1"}
<script>
    let list_edit = "{:url('fastedit')}";
    $(`._fast`).blur(function () {
        let _this = this,
            val = $(_this).val(),
            d = $(_this).data();
        if(val == d.value){
            return;
        }
        wormui.postUrl(list_edit,{id:d.id,filed:_this.name,value:val},function (res) {
            layer.msg(res.msg);
            if (res.code == '0'){
                $(_this).val(d.value);
            }
        });
    });
</script>
{/eq}
{// 检测便捷开关}
{eq name="list_base.list_switch|default='0'" value="1"}
<script>
    $(`._switch`).click(function () {
        let _this = this,
            d = $(_this).data();
        wormui.postUrl("{:url('fastswitch')}",{id:d.id,filed:d.name},function (res) {
            layer.msg(res.msg);
            if (res.code == '1'){
                let i = $(_this).find(`i.cx-icon`),
                    has = $(i).hasClass(`cx-iconzhengque`);
                $(i).removeClass(has ? `cx-iconzhengque cx-text-green` : `cx-iconjinzhi cx-text-red`).addClass(has ? `cx-iconjinzhi cx-text-red` : `cx-iconzhengque cx-text-green`);
            }
        });
    });
</script>
{/eq}
{// 检测快速还原}
{eq name="list_base.edit|default='0'" value="1"}
<script>
    $(`._trash`).click(function () {
        let _this = this,
            d = $(_this).data();
        wormui.postUrl("{:url('trashone')}",{id:d.id},function (res) {
            layer.msg(res.msg);
            if (res.code == '1'){
                window.location.reload();
            }
        });
    });
</script>
{/eq}
{// 检测快速删除}
{eq name="list_base.del|default='0'" value="1"}
<script>
$(`._delete`).click(function () {
    let _this = this,
        d = $(_this).data();
    layer.confirm("删除后无法恢复，确定要删除吗？",{icon:3,title:"警告"},function (index) {
        let url = d.uri ? d.uri : "{:url('del')}";
        wormui.postUrl(url,{id:d.id,_method:"delete"},function (res) {
            layer.msg(res.msg,{},function () {
                if (res.code == '1'){
                    {eq name="list_base.list_fold|default='0'" value="1"}
                    window.location.reload();
                    {else /}
                    $(_this).closest(`tr`).remove();
                    {/eq}
                }
            });
        });
        layer.close(index);
    });
});
</script>
{/eq}
{notempty name="list_top"}
<script>
    let ids_list = $("input[name='_ids[]']");
    $(`._checkbox`).click(function () {
        let _this = this,
            chec_type = $(_this).is(':checked');
        $.each(ids_list,function (index,item) {
            $(item).prop("checked",chec_type ? true : false);
        });
    });
    ids_list.click(function () {
        let n = 0;
        $.each(ids_list,function (index,item) {
            $(item).is(':checked') ? n++ : '';
        });
        $(`._checkbox`).prop("checked",n === ids_list.length ? true : false);
    });
    $(`a[data-ids]`).click(function () {
        let ids = [],
            d = $(this).data();
        $.each(ids_list,function (index,item) {
            $(item).is(':checked') ? ids.push(item.value) : '';
        });
        if(ids.length === 0){
            layer.msg('你还没有选择内容');
            return false;
        }
        wormui.postUrl(d.uri,{ids:ids},function (res) {
            layer.msg(res.msg);
            if (res.code == '1'){
                window.location.reload();
            }
        });
    });
</script>
{/notempty}
{/block}